//
//  VRUserAccount.swift
//  Sina_MicoBlog
//
//  Created by Lee on 16/5/14.
//  Copyright © 2016年 Lee.C. All rights reserved.
//
//在这里进行一系列的归档解档还有字典转模型.
//从code拿到的数据进行字典转模型,解档和归档的两个方法必须实现.

import UIKit

class VRUserAccount: NSObject, NSCoding {

    //从code拿到的数据进行字典转模型
    var access_token: String?
    //这里的expires_in不是string,而是一个number
    var expires_in: NSTimeInterval = 0 {
        didSet {
        expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    ///过期时间,方便比较时间
    var expiresDate: NSDate?
    ///授权UID
    var uid: String?
    
    //设置构造方法
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    //抛异常
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    ///归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
    }
    /// 解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
    }
    
}
